package org.exmaple;

import java.util.Random;

/**
 * 生产者消费-等待通知机制
 */
public class Test1 {
    public static void main(String[] args) {
        Test1 test = new Test1();
        Stack stack = test.new Stack();
        Producer p = test.new Producer(stack);
        Consumer c = test.new Consumer(stack);

        p.start();
        c.start();

        //不停的通知，需要不停的判断
//		while(true){
//			synchronized (stack) {
//				stack.notifyAll();
//			}
//		}
    }

    public class Producer extends Thread {
        private Stack stack;

        public Producer(Stack stack) {
            this.stack =stack;
        }

        @Override
        public void run() {
            while(true){
                System.out.println("==============Producer============");
                char c =(char)('a'+new Random().nextInt(26));
                synchronized (stack) {
                    System.out.println("==============Producer>synchronized============");
                    if(stack.isFull()){
                        try {
                            System.out.println("Producer>>wait>>");
                            stack.wait();
                        } catch (InterruptedException e) {
                        }
                    }
                    stack.push(c);
                    System.out.println("压入<<"+c);
                    stack.notifyAll();
                    System.out.println("Producer>>notifyAll>>");
                }
            }
        }
    }

    public class Consumer extends Thread {
        private Stack stack;

        public Consumer(Stack stack) {
            this.stack =stack;
        }

        @Override
        public void run() {
            while(true){
                System.out.println("==============Consumer============");
                synchronized (stack) {
                    System.out.println("==============Consumer>synchronized============");
                    //把if改为while循环判断是否存入数据
                    while(stack.isEmpty()){
                        try {
                            System.out.println("Consumer>>wait>>");
                            stack.wait();
                        } catch (InterruptedException e) {
                        }
                    }
                    char c =(char) stack.pop();
                    System.out.println("弹出>>"+c);
                    stack.notifyAll();
                    System.out.println("Consumer>>notifyAll>>");
                }
            }
        }
    }

    public class Stack{
        private char[] a =new char[5];
        private int index;

        public void push(char c){
            if(isFull()){
                return;
            }
            a[index] =c;
            index++;
        }

        public boolean isFull() {
            return index==5;
        }

        public char pop(){
            if(isEmpty()){
                return ' ';//空格表示空，没有数据
            }
            index--;
            char c =a[index];
            return c;
        }

        public boolean isEmpty() {
            return index==0;
        }
    }
}
